import {List} from 'protomap';

const randomChoice = (list, {number}={}) => {
  if (Number.isInteger(number) && number > 0){
    const res = [];
    for (let i = 0; i < number; i++) {
      res.push(list[Math.floor(list.length * Math.random())]);
    }
    return res;
  } else {
    return list[Math.floor(list.length * Math.random())];
  }
}

const createRec = (cols) => {
  
  const entries = [];
  for (let [key, {type}] of Object.entries(cols)) {
    entries.push([key, type === 'number' ? Math.random() : Math.random().toString(36)]);
  }

  return Object.fromEntries(entries);
}

export const createRandomList = ({baseList, cols, records=100, stopProb=0.5}={}) => {
  
  let list = new List();
  list.add({records: baseList || []});
  
  console.log('creat', list)
  let currList = list;

  for (let i = 0; i < records; i++) {
    while(true) {
      if(currList.length === 0 || Math.random() > stopProb) {
        currList.add({records: createRec(cols)});
        break;
      } else if (Math.random() > stopProb){
        currList.add({records: createRec(cols)});
        break;
      } else {
        currList = randomChoice(currList).succ;
      }
    }
  }

  const randomVowels = [...Array(100)].map(() => Math.random().toString(35).slice(7, 10));

  list.trav((rec, path) => {

    if (rec.code === undefined) {
      rec.code = path.map(i => randomVowels[i]).join('');
    }

    // if (rec.desc === undefined) {
    //   rec.desc = Math.random().toString(35).slice(7, 12);
    // }
  })

  return list.nest({nestColumn: 'code'});
  
}